package com.tys.fmod;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;

/**
 * Created by tys on 2017/9/5.
 */

public class Fmod {

    private static Context context;

    public static void init(Context context) {
        Fmod.context = context;
    }

    public static void close() {
        Fmod.context = null;
    }

    public static boolean checkInit() {
        return context != null;
    }

    /**
     * 低延迟
     *
     * @return
     */
    public static boolean supportsLowLatency() {
        if (context != null) {
            int var0 = getOutputBlockSize();
            boolean var1 = context.getPackageManager().hasSystemFeature("android.hardware.audio.low_latency");
            boolean var2 = context.getPackageManager().hasSystemFeature("android.hardware.audio.pro");
            boolean var3 = var0 > 0 && var0 <= 512;
            Log.e("fmod", "FMOD::supportsLowLatency : Low latency = " + var1 + ", Pro Audio = " + var2 + ", Acceptable Block Size = " + var3 + " (" + var0 + ")");
            return var3;
        } else {
            return false;
        }
    }

    public static int getOutputSampleRate() {
        if (context != null && Build.VERSION.SDK_INT >= 17) {
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            String sampleRate = am.getProperty("android.media.property.OUTPUT_SAMPLE_RATE");
            if (sampleRate != null) {
                return Integer.parseInt(sampleRate);
            }
        }
        return 0;
    }

    public static int getOutputBlockSize() {
        if (context != null && Build.VERSION.SDK_INT >= 17) {
            AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
            String framesPerBuffer = am.getProperty("android.media.property.OUTPUT_FRAMES_PER_BUFFER");
            if (framesPerBuffer != null) {
                return Integer.parseInt(framesPerBuffer);
            }
        }
        return 0;
    }





}
